﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Website
{
    public partial class register : Form
    {
        public register()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void LoginRegister_Click(object sender, EventArgs e)
        {
            Hide();
            login mylogin = new login();
            mylogin.StartPosition = FormStartPosition.CenterParent;
            mylogin.ShowDialog();
        }

        private void Registerbtn_Click(object sender, EventArgs e)
        {
            string UserId = registerUserId.Text;
            string Password = registerPassword.Text;
            string UserName = registerUserName.Text;
            string Repassword = reregisterPassword.Text;
            if (registerUserName.Text == "")
            {
                MessageBox.Show("请输入用户名", "注册失败");
                registerUserName.Focus();
            }
            else if (registerUserId.Text == "")
            {
                MessageBox.Show("请输入账号", "注册失败");
                registerUserId.Focus();
            }
            else if (registerPassword.Text == "")
            {
                MessageBox.Show("请输入密码", "注册失败");
                registerPassword.Focus();
            }
            else if (reregisterPassword.Text == "")
                MessageBox.Show("请确认密码", "注册失败");
            else
            {
                string myConn = "server=PC-202006091644\\SQLEXPRESS;Initial Catalog=website;User ID=sa;Password=123456;Integrated Security=True";
                SqlConnection sqlConnection = new SqlConnection(myConn);    //实例化连接对象  
                sqlConnection.Open();
                string sql = "select UserId from [user] where UserId='" + UserId + "'";//SQL语句实现表数据的读取
                SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();

                if (sqlDataReader.HasRows)
                {
                    sqlConnection.Close();
                    MessageBox.Show("该用户名已存在，请重新注册", "注册失败");
                    registerUserId.Text = "";
                    registerPassword.Text = "";
                    reregisterPassword.Text = "";
                    registerUserName.Text = "";
                    registerUserId.Focus();     //指定光标在哪个textBox处闪烁
                }
                else
                {
                    if (Password == Repassword)//两次输入的密码一致  
                    {
                        sqlConnection.Close();
                        string myConn2 = "server=PC-202006091644\\SQLEXPRESS;Initial Catalog=website;User ID=sa;Password=123456;Integrated Security=True";
                        SqlConnection sqlConnection2 = new SqlConnection(myConn2);   //实例化连接对象  
                        sqlConnection.Open();
                        Password = GetMD5(Password);
                        string sql2 = "INSERT INTO [user](UserId,Password,UserName) VALUES('" + UserId + "','" + Password + "','" + UserName + "')";//SQL语句向表中写入数据  
                        SqlCommand sqlCommand2 = new SqlCommand(sql2, sqlConnection);
                        sqlCommand2.ExecuteNonQuery();
                        sqlConnection2.Close();
                        DialogResult dr = MessageBox.Show("是否返回主界面", "注册成功", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dr == DialogResult.Yes)//打开注册界面
                        {
                            login form2 = new login();
                            this.Hide();
                            form2.ShowDialog();
                            Application.ExitThread();
                        }
                        else
                        {
                            registerUserId.Text = "";
                            registerUserName.Text = "";
                            registerPassword.Text = "";
                            reregisterPassword.Text = "";
                            this.Show();
                        }

                    }
                    else
                    {
                        MessageBox.Show("两次输入密码不一致", "错误信息");
                        registerUserId.Text = "";
                        registerUserName.Text = "";
                        registerPassword.Text = "";
                        reregisterPassword.Text = "";
                    }
                }
            }
        }

        public static string GetMD5(String input)   //MD5算法，输入一段字符串，输出一段字符串
        {
            string cl = input;
            string pwd = "";
            MD5 md5 = MD5.Create();//实例化一个md5对像
            // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 

                pwd = pwd + s[i].ToString("X");

            }
            return pwd;
        }

        private void registerUserId_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                registerPassword.Focus();
            }
        }

        private void registerPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                reregisterPassword.Focus();
            }
        }

        private void reregisterPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                registerUserName.Focus();
            }
        }

        private void registerUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                LoginRegister_Click(null, null);
            }
        }
    }
}
